import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login'
import Home from '@/components/Home'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: 'login'
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/home',
      name: 'home',
      component: Home
    }
  ]
})


// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 表示将要访问的路径
  // from 表示从哪个路径盅而来
  // next 是一个函数,表示放行
  // next() 直接放行  next('/login') 跳转到指定页面
  if (to.path === '/login') return next()
  const tokenstr = window.sessionStorage.getItem('token');
  if (!tokenstr) return next('/login')
  next()
})

export default router
